/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nl.hva.web.mvcwebapp.doa;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import nl.hva.web.mvcwebapp.models.Course;
import nl.hva.web.mvcwebapp.models.News;
import nl.hva.web.mvcwebapp.service.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

/**
 *
 * @author Tom
 */
public class NewsDAO {

    public NewsDAO() {
    }
    // Get all News item from a Course with a max amount
    public static List<News> getNewsAllMax( int amount) {
        
        AnnotationConfiguration config = new AnnotationConfiguration();
        config.addAnnotatedClass(News.class);
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        session.beginTransaction();
        Query queryResult = session.createQuery("FROM News 'ORDER BY newsdate  DESC");
        queryResult.setMaxResults(amount);
        List<News> news_list = queryResult.list();
        tx.commit();
        return news_list;
    }
    // Get all News item from course or just all News items
    public static List<News> getAllNews(int course_id) {
        
        AnnotationConfiguration config = new AnnotationConfiguration();
        config.addAnnotatedClass(News.class);
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        session.beginTransaction();
        Query queryResult = session.createQuery("FROM News WHERE course_courseid = '"+course_id+"'");
        List<News> news_list = queryResult.list();
        tx.commit();
        return news_list;
    }
    //Get News item object
    public static News getNews(int news_id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        News news = (News) session.get(News.class, news_id);
        tx.commit();

        return news;
    }
    // Insert a New news item
    public static ArrayList insertNews(HttpServletRequest request, Course course) {
        ArrayList validation_errors = new ArrayList();
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        News news = getNewsFromRequest(request, course);
        session.save(news);
        tx.commit();
        return validation_errors;
    }
    // Update a News item
    public static ArrayList updateNews(HttpServletRequest request, Course course) {
        ArrayList validation_errors = new ArrayList();
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        News news = getNewsFromRequest(request, course);
        session.update(news);
        tx.commit();
        return validation_errors;
    }
    // Delete News item
    public static void deleteNews(int news_id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        News tempNews =(News) session.load(News.class, news_id);
        
        session.delete(tempNews);
        tx.commit();
    }

 
    
    private static News getNewsFromRequest(HttpServletRequest request, Course course) {
        News n = new News();

        if (request.getParameter("newsid") != null && !request.getParameter("newsid").isEmpty()) {
            n.setNewsid(Integer.parseInt(request.getParameter("newsid")));
        }       
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        Date date = new Date();
        n.setNewsdate(dateFormat.format(date));
        if (request.getParameter("newstitle") != null && !request.getParameter("newstitle").isEmpty()) {
            n.setNewstitle(request.getParameter("newstitle"));
        }
        if (request.getParameter("newscontent") != null && !request.getParameter("newscontent").isEmpty()) {
            n.setNewscontent(request.getParameter("newscontent"));
        }
        if (request.getParameter("newsglobal") != null && !request.getParameter("newsglobal").isEmpty()) {
            n.setNewsglobal(Integer.parseInt(request.getParameter("newsglobal")));
        }
        if (course != null) {
            n.setCourse(course);
        }
        return n;
    }
    
}
